/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.orderView.dish;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.jdialog.BorderJPanel;
import com.ruipengkj.component.jdialog.IDialog;
/**
 * 说明：
 * 整单折-填写折扣
 */
public class OrderDiscounDialog extends IDialog {

	private static final long serialVersionUID = 1L;

	private JTextField discoun;//标示输入框(相当于次订单的名字)
	private JButton sureButton;//确定
	private JButton cancleButton;//取消
	
	public boolean isSuccess = false;//成功
	
	public OrderDiscounDialog() {
		super((JDialog)null, "填写折扣", true);
		this.setSize(350, 150);
		init();
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
	
	private void init() {
		discoun = new JTextField(20);
		discoun.addKeyListener(this);
		discoun.addMouseListener(this);
		Util.setSize(discoun, 200, 30);
		discoun.setFont(Util.FONT_20_PLAIN);
		
		sureButton = new JButton("确  定");
		cancleButton = new JButton("取  消");
		
		sureButton.setFont(Util.FONT_20_PLAIN);
		cancleButton.setFont(Util.FONT_20_PLAIN); 
		
		sureButton.addActionListener(this);
		cancleButton.addActionListener(this);
		
		JPanel mainJPanel  = new BorderJPanel();
		mainJPanel.setBackground(Color.white);
		mainJPanel.setLayout(new GridLayout(2, 1));
		mainJPanel.add(new JPanelFlowLayoutComponent(new Component[]{
				new JLabel("折扣："), discoun
		}, FlowLayout.CENTER));
		mainJPanel.add(new JPanelFlowLayoutComponent(new Component[]{
				sureButton, cancleButton
		}, FlowLayout.CENTER));
		this.add(mainJPanel);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == sureButton) {
			if (!MatcherFormatUtil.MatcherPriceScheme(discoun.getText()) || Double.parseDouble(discoun.getText()) > 1 || Double.parseDouble(discoun.getText()) == 0) {
				Util.showMessageDialog("折扣必须大于0，小于1且最多2位小数！");
				return;
			}
			isSuccess = true;
			this.dispose();
		}
		this.dispose();
	}
	
	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			if (!MatcherFormatUtil.MatcherPriceScheme(discoun.getText()) || Double.parseDouble(discoun.getText()) > 1 || Double.parseDouble(discoun.getText()) == 0) {
				Util.showMessageDialog("折扣必须大于0，小于1且最多2位小数！");
				return;
			}
			isSuccess = true;
			this.dispose();
		} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
			this.dispose();
		}
	
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == discoun) {
			new PayJPopupMenu(discoun).show(discoun, discoun.getWidth(), 0);
		}
	}
	
	public String getText() {
		return discoun.getText();
	}
	
	public static void main(String[] args) {
		new OrderDiscounDialog();
	}
}
